package com.itany.corejava.code08_集合.set集合;

/**
 * @author MissChen
 * @date 2024年03月05日 13:41
 */
public class User implements Comparable<User>{
    private String username;
    private int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 比较方法，指定比较规则
     * 此时根据年龄进行升序排列
     * @param user
     * @return
     */
    @Override
    public int compareTo(User user) {
        //升序排列
        //当前对象的年龄大于参数对象，返回值大于0
        if(this.age>user.getAge()){
            return 1;
        }else if(this.age<user.getAge()){
            return -1;
        }else{
            //如果年龄相同，则根据用户名的长度进行比较
            //降序
            if(this.username.length()>user.getUsername().length()){
               return -1;
            }else if(this.username.length()<user.getUsername().length()){
                return 1;
            }else{
                return 0;
            }

        }

    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
